package com.itheima.servlet;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.Enumeration;
import java.util.Map;
import java.util.Set;

@WebServlet(name="studnetServletDemo1",value = "/studnetServletDemo1")
public class StudentServletDemo1 extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        System.out.println("--------根据具体名称获取一个值------");
        //获取参数获取一个值
        String username = req.getParameter("username");
        System.out.println(username);


        System.out.println("------根据具体名称获取多个值--------");
        //获取多个值
        String[] hobbies = req.getParameterValues("hobby");
        for (String hobby : hobbies) {
            System.out.println(hobby);
        }

        System.out.println("-------获取所有参数名称-------");
        //获取所有名称
        Enumeration<String> names = req.getParameterNames();
        while (names.hasMoreElements()) {
            String s = names.nextElement();
            System.out.println(s);
        }

        System.out.println("-------获取所有参数的键值对-------");
        //获取所有参数的键值对
        Map<String, String[]> maps = req.getParameterMap();
        Set<String> keys = maps.keySet();
        for (String key : keys) {
            String[] values = maps.get(key);
            System.out.print(key+":");
            for (String value : values) {
                System.out.print(value+" ");
            }
            System.out.println();
        }

        /*PropertyDescriptor pd = new PropertyDescriptor();
        pd.getWriteMethod();*/

        System.out.println("-----------");
        String username1 = req.getParameter("username");
        System.out.println(username1);
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doGet(req,resp);
    }
}
